/* hwInterfaceBase.c */

/************************************************************************************
*                    Copyright Aquantia Corporation
*                              Freeware
*
* Description:
*
*   This file contains the bare minimum required by TCP server from local hwInterface
*
************************************************************************************/

/*! \file
    This file contains the bare minimum required by TCP server from local hwInterface
*/

#include "hwInterfaceBase.h"
#include "userPhyAccess.h"
#include "stdio.h"

#ifdef __cplusplus
  extern "C" {
#endif

static char* adapterNames[AQ_USERPA_NUM_ADAPTERS];
static unsigned int isInitialized = 0;
static unsigned int defaultAdapterNumber = 0;

/*// Defined in userPhyAccess.c*/
extern char AQ_USERPA_AdapterNamesBuffer[AQ_USERPA_NUM_ADAPTERS][AQ_USERPA_MAX_ADAPTER_NAME_LENGTH+1];

/*! \addtogroup Local_HW_Interface Local HW Interface
HW Interface functions are required by the TCP server for interfacing the system hardware. The body of these functions needs to be written by the system designer, as the method of interfacing will be unique to the target system. They are designed to be generic read and write access functions.
 @{*/

/* At a bare minimum, the following are required from the local hwInterface: */

/* This function returns the HW_ErrorFlag.  If set, an error has occurred, and the error message can be read by
    HW_Error() */
unsigned int HW_Fail ()
{
  return 0;
}

/* This function returns the error message. */
char* HW_Error ()
{
  return NULL;
}

/* This function performs the initialization of the system to determine the list of devices.  Successful
    initialization returns 1, and failure returns 0 */
unsigned int HW_Initialize ()
{
  if (!isInitialized)
  {
    unsigned int i;

    for (i = 0; i < AQ_USERPA_NUM_ADAPTERS; ++i)
    {
      adapterNames[i] = AQ_USERPA_AdapterNamesBuffer[i];
    }
    defaultAdapterNumber = 0;
    isInitialized = 1;
  }
  return 1;
}

/* This function returns the number of devices in the system */
unsigned int HW_NumberOfDevices ()
{
  return AQ_USERPA_NUM_ADAPTERS;
}

/* This function returns the list of device names in the system */
char** HW_DeviceList ()
{
  return adapterNames;
}

/* This function opens an adapter, where the adapter number references the adapter found in adapter list */
unsigned int HW_OpenAdapter (unsigned int adapter)
{
  if (adapter > AQ_USERPA_NUM_ADAPTERS)
    return 0;
  return 1;
}

/* This function closes a specific adapter, but does not close the interface */
unsigned int HW_CloseAdapter (unsigned int adapter)
{
  if (adapter > AQ_USERPA_NUM_ADAPTERS)
    return 0;
  return 1;
}

/* This function closes the interface */
void HW_Close ()
{
  unsigned int i;

  for (i = 0; i < AQ_USERPA_NUM_ADAPTERS; ++i)
  {
    HW_CloseAdapter(i);
  }
  defaultAdapterNumber = 0;

  isInitialized = 0;
}

/* This function performs a write on the specified adapter */
void HW_Write (unsigned int adapter, unsigned int PHY_ID, unsigned int MMD, unsigned int address, unsigned int data)
{
  AQ_USERPA_Register_Write(adapter, PHY_ID, MMD, address, data);
}

/* This function performs a read on the specified adapter */
unsigned int HW_Read (unsigned int adapter, unsigned int PHY_ID, unsigned int MMD, unsigned int address)
{
  return AQ_USERPA_Register_Read(adapter, PHY_ID, MMD, address);
}

/* Returns the current default adapter number. */
unsigned int HW_GetDefaultAdapter ()
{
  return defaultAdapterNumber;
}

/* Explicitly designates adapterNumber as the current default adapter.  adapterNumber must be 
 * a valid index into the list of adapters.  The adapter must be open.   A return value of 1  
 * signifies success, 0 failure.  In case of failure, the default adapter will not be changed. */
unsigned int HW_SetDefaultAdapter (unsigned int adapterNumber)
{
  defaultAdapterNumber = adapterNumber;
  return 1;
}

/*! Returns whether buffered read/write operations are supported by the implementation */
unsigned int AQ_API_MDIO_SupportsBlockOperations ()
{
  return 0;
}

/*@}*/

#ifdef __cplusplus
  }
#endif

